package pt.isec.gpsgroup5.moneybox;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Toast;


public class NewObjective extends Activity {

	private DBAdapter db;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_objective);
		
		db = new DBAdapter(this);
		db.open(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_new_objective, menu);
		return true;
	}
	
	public void onNewObjectiveSave(View v){
		String name;
		float finalAmount;
		int priority;
		int year, month, day;
		int id;
		
		EditText newObjectiveName = (EditText) findViewById(R.id.newObjectiveName);
		name = newObjectiveName.toString();
		
		EditText newObjectiveFinalAmount = (EditText) findViewById(R.id.newObjectiveFinalAmount);
		finalAmount = Integer.parseInt( newObjectiveFinalAmount.toString() );
		
		DatePicker dp = (DatePicker) findViewById(R.id.newObjectiveDeadLine);
		year = dp.getYear();
		month = dp.getMonth();
		day = dp.getDayOfMonth();
		
		RatingBar rbpriority = (RatingBar) findViewById(R.id.newObjectivePriority);
		priority = (int) rbpriority.getRating();
		
		if( !db.isNameRepeated(name) ){
			id = db.getNextAvailableID();
			Objective obj = new Objective(id, name, finalAmount, 0, year, month, day, priority, 1);
			db.createObjective(obj);
			Toast.makeText(this, R.string.newObjectiveSuccess, Toast.LENGTH_LONG).show();
		
		} else {
			Toast.makeText(this, R.string.repeatedName, Toast.LENGTH_LONG).show();
		}
	}

}
